package com.mmlc.meimei.lichengkongjian.classify;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.responsejavabean.RequestError;
import com.mmlc.inter.InterActivity;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.network.filter.GetPaymentRequestFilter;
import com.mmlc.network.responsebean.GetPaymentResponseBean;
import com.mmlc.utils.ViewUtils;

import java.util.ArrayList;
import java.util.List;

public class ModePaySelectActivity extends InterActivity {

    @Override
    protected int initContentView() {
        return R.layout.activity_mode_pay_select;
    }

    String companyName;
    String type;
    @Override
    protected void initExtra() {
        super.initExtra();
        selectedMode = getIntent().getIntExtra("mode", 0);
        type = getIntent().getStringExtra("type");

    }

    ListView list_payment;
    PaymentAdapter adapter;
    TextView tv_title;
    @Override
    protected void initView() {
        tv_title = ViewUtils.getView(activity,R.id.tv_title);
        tv_title.setText("支付方式");
        list_payment = ViewUtils.getView(activity, R.id.list_payment);
        adapter = new PaymentAdapter(context, payments);
        list_payment.setAdapter(adapter);
    }

    List<GetPaymentResponseBean.DataBean> payments = new ArrayList<>();

    @Override
    protected void initInfo() {
        GetPaymentRequestFilter filter = new GetPaymentRequestFilter(activity);
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<GetPaymentResponseBean>() {
            @Override
            public void onSuccess(GetPaymentResponseBean result) {
                if (result == null) result = new GetPaymentResponseBean();
                if (result.getCode() == 0 && result.getData() != null) {
                    payments.addAll(result.getData());
                    adapter.notifyDataSetChanged();
                    adapter.setSelectedMode(selectedMode);
                }
            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                initInfo();
            }
        });
    }

    @Override
    protected void initEvent() {
        ViewUtils.getView(activity, R.id.iv_back).setOnClickListener(this);
        ViewUtils.getView(activity, R.id.tv_verify).setOnClickListener(this);

        list_payment.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                selectedMode = position;
                adapter.setSelectedMode(selectedMode);

            }
        });
    }

    int selectedMode = 0;

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.iv_back:
            case R.id.tv_verify:
                Intent intent = new Intent();
                selectedMode = adapter.getSelectedMode();
                if(!TextUtils.isEmpty(type) && type.equals("payagain")){
                    String pay_code = payments.get(selectedMode).getPay_code();
                    String pay_name = payments.get(selectedMode).getPay_name();
                    intent.putExtra("code",pay_code);
                    intent.putExtra("name",pay_name);
                    setResult(200, intent);
                }else {
                    intent.putExtra("mode", selectedMode);
                    setResult(200, intent);
                }
                finish();
                break;
        }
    }

}
